package com.zzsy.carosshopprojects.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.bean.RedPageBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DelCouponAdapter extends ViewHolderRecyclerAdapter<RedPageBean> {
    private Context context;
    private List<RedPageBean> mAllData;
    private CheckBox mCheckboxAll;
    private TextView mTvShopcartTotal;
    private TextView mTvCount;

    public DelCouponAdapter(Context context, ArrayList<RedPageBean> listData, int layoutId, CheckBox checkboxAll, TextView tvZong, TextView tvNum) {
        super(context, listData, layoutId);
        this.context = context;
        this.mAllData = listData;
        this.mCheckboxAll = checkboxAll;
        this.mTvShopcartTotal = tvZong;
        this.mTvCount = tvNum;
        showCount();
        ShowTotaLPrice();
        //设置点击事件
        //设置点击事件
        setListener();
        //校验是否全选
        checkAll();
    }

    private void setListener() {
        setOnItemClickListener(new DelCouponAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //根据位置找到对应的bean
                RedPageBean goodsBean = mAllData.get(position);
                //设置取反状态
                goodsBean.setSelected(!goodsBean.isSelected());
                //刷新数据
                notifyItemChanged(position);
                //校验是否全选
                checkAll();
                //重新计算
                ShowTotaLPrice();
                //重新统计个数
                showCount();
            }
        });

        //设置全选费全选的点击事件
        mCheckboxAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到状态
                boolean isChecked = mCheckboxAll.isChecked();
                //根据状态设置全选和非全选

                checkAll_none(isChecked);

                //计算总价格
                ShowTotaLPrice();
                //计算个数
                showCount();
            }
        });
    }

    /**
     * 校验是否全选
     */
    public void checkAll() {
        if (mAllData != null && mAllData.size() >= 0) {
            int number = 0;
            for (int i = 0; i < mAllData.size(); i++) {
                RedPageBean goodsBean = mAllData.get(i);
                if (!goodsBean.isSelected()) {
                    //非全选
                    mCheckboxAll.setChecked(false);
                } else {
                    //选中的
                    number++;
                }
            }
            if (number == mAllData.size()) {
                //全选
                mCheckboxAll.setChecked(true);
            }
        } else {
            mCheckboxAll.setChecked(false);
        }
    }

    /**
     * 设置全选和非全选
     */
    public void checkAll_none(boolean isChecked) {
        if (mAllData != null && mAllData.size() >= 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                RedPageBean goodsBean = mAllData.get(i);
                goodsBean.setSelected(isChecked);
                notifyItemChanged(i);
            }

        }
    }

    /**
     * 总合计
     */
    public void ShowTotaLPrice() {
        mTvShopcartTotal.setText("合计:￥" + getTtotalPrices());
    }

    private void showCount() {
        mTvCount.setText("个数：" + getCount());
    }

    /**
     * 计算总价格
     */
    private String getTtotalPrices() {
        double totalPrice = 0;
        if (mAllData != null && mAllData.size() > 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                RedPageBean goodsBean = mAllData.get(i);
                if (goodsBean.isSelected()) {
                    double newPrice = ConverterHelper.toBigDecimal(goodsBean.getPreAmount()).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    totalPrice = totalPrice + newPrice;
                }
            }
        }
        return ConverterHelper.toBigDecimal(totalPrice).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    //设置个数
    private String getCount() {
        int count = 0;
        if (mAllData != null && mAllData.size() > 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                RedPageBean goodsBean = mAllData.get(i);
                if (goodsBean.isSelected()) {
                    count++;
                }
            }
        }
        return String.valueOf(count);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.adapter_delcoupon, null);
        ScreenAdapterTools.getInstance().loadView(view);
        return view;
    }

    @Override
    public void bindViewDatas(ViewHolder helper, RedPageBean listBean, int positions) {
        ImageView cb_gov = helper.getView(R.id.cb_gov);
        if (listBean.isSelected()) {
            cb_gov.setImageResource(R.mipmap.coupon_check);
        } else {
            cb_gov.setImageResource(R.mipmap.coupon_uncheck);
        }
        double money = ConverterHelper.toBigDecimal(listBean.getPreAmount()).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        helper.setText(R.id.tv_yg_money, "￥" + money);
        if (!StringUtils.isEmpty(listBean.getTimeOut())) {
            helper.setText(R.id.tv_time_gq, "过期时间:" + listBean.getTimeOut());
        }
        if (!StringUtils.isEmpty(listBean.getCouponName())) {
            helper.setText(R.id.tv_counp_name, listBean.getCouponName());
        }
        helper.getConvertView().setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(positions);
            }
        });
    }

    /**
     * 监听者
     */
    public interface OnItemClickListener {
        /**
         * 点击某一条的时候
         */
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
